package UI;

import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import oocis.user.*;

public class SignInUI extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JTextField jTextField = null;

	private JPasswordField jPasswordField = null;

	private JButton jButton = null;

	private JButton jButton1 = null;

	private User signedUser = null;

	/**
	 * This is the default constructor
	 */
	public SignInUI() {
		super();
		initialize();
	}

	public User getUser(){
		return signedUser;
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("Sign in");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(15, 90, 124, 20));
			jLabel2.setText("Password :");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(15, 60, 120, 20));
			jLabel1.setText("User Name :");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(15, 20, 200, 20));
			jLabel.setFont(new Font("Arial", Font.BOLD, 18));
			jLabel.setText("Please Sign In :");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(getJPasswordField(), null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJButton1(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(150, 60, 123, 20));
		}
		return jTextField;
	}

	/**
	 * This method initializes jPasswordField
	 * 
	 * @return javax.swing.JPasswordField
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(150, 90, 121, 20));
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(30, 130, 100, 30));
			jButton.setText("Sign In");
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					String _username = jTextField.getText();
					String _password = jPasswordField.getText();
					
					userManage um = new userManage();
					System.out.println("haha1");
					if (um.checkname(_username)){
						System.out.println("Haha2");
						if (um.login(_username, _password)){
							JOptionPane.showMessageDialog(null, "Signed Successful, enjoy it.");
							System.out.println("haha3");
							signedUser.setName(_username);
							close();
						}else{
							JOptionPane.showMessageDialog(null, "Password Error, type it again please!", "Error", JOptionPane.INFORMATION_MESSAGE);
						}
					}else{
						JOptionPane.showMessageDialog(null, "User not exist, please check it!", "Error", JOptionPane.INFORMATION_MESSAGE);
						
					}
					// TODO Auto-generated Event stub mouseClicked()
				}
			});
		}
		return jButton;
	}

	private void close() {
		this.dispose();
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(155, 130, 100, 30));
			jButton1.setText("Cancel");
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					close(); // TODO Auto-generated Event stub mouseClicked()
				}
			});
		}
		return jButton1;
	}

} // @jve:decl-index=0:visual-constraint="294,37"
